package com.course.masterservice.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.course.component.http.BasePageResponse;
import com.course.component.util.CopyUtil;
import com.course.component.util.SnowFlake;
import com.course.dto.reqs.RequestPermission;
import com.course.dto.resp.ResponsePermissiom;
import com.course.entity.Articles;
import com.course.entity.Category;
import com.course.entity.Permission;
import com.course.masterservice.PermissionMasterService;
import com.course.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Copyright: Copyright (c)  林中小鸟  All rights reserved.
 * @author: moon
 * @date: 2022/9/20 13:50
 * @Description: **********************
 */
@Service
public class PermissionMasterServiceImpl implements PermissionMasterService {

    private final Map<String, Integer> typeMap = new HashMap<>();
    @Autowired
    private IPermissionService permissionService;

    @Override
    public Boolean update(RequestPermission requestPermission) {
        Permission updateData = CopyUtil.copy(requestPermission,Permission.class);
        if(ObjectUtils.isEmpty(updateData.getId())){
            updateData.setId(new SnowFlake().nextId());
        }
        updateData.setMetaJson(updateData.getMeta().toJSONString());
        updateData.setUri(requestPermission.getPath());
        String strType = (String) requestPermission.getMeta().get("type");
        typeMap.put("doc",0);
        typeMap.put("menu",1);
        typeMap.put("iframe",4);
        typeMap.put("link",3);
        typeMap.put("button",2);
        updateData.setType(typeMap.get(strType));
        updateData.setMetaJson(requestPermission.getMeta().toJSONString());
        System.out.println(updateData);
        return permissionService.saveOrUpdate(updateData);
    }

    @Override
    public Boolean del(RequestPermission requestPermission) {
        if(requestPermission.getIds()!=null){
            System.out.println(requestPermission.getIds());
            return permissionService.removeByIds(requestPermission.getIds());
        }else{
            return permissionService.removeById(requestPermission.getId());
        }
    }

    @Override
    public BasePageResponse<ResponsePermissiom> list(RequestPermission requestPermission) {
        //使用分页
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        IPage<Permission> list = permissionService.page(new Page<>(requestPermission.getPage().intValue(),requestPermission.getPageSize().intValue()),queryWrapper);
        List<Permission> records = list.getRecords();
        for(Permission item:records){
            item.setMeta(JSON.parseObject(item.getMetaJson()));
        }
        return new BasePageResponse(list.getTotal(),records.stream().map(item-> CopyUtil.copy(item,ResponsePermissiom.class)).collect(Collectors.toList()));
    }
}
